package com.ihighest.app.infox.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.myview.CalendarCard;
import com.ihighest.app.infox.myview.CalendarCard.OnCellClickListener;
import com.ihighest.app.infox.myview.CalendarViewAdapter;
import com.ihighest.app.infox.myview.CustomDate;
import com.ihighest.app.infox.myview.SettingSimpleView;
import com.ihighest.app.infox.myview.ThiredTextItemView;
import com.umeng.socialize.utils.Log;

/**
 * 任务中心界面
 * 
 * @author lkx Created at 2015-10-9 上午9:18:07
 * @version 1.0
 */
public class AssignmentCenteringActivity extends BaseActivity implements OnCellClickListener, OnClickListener {
	private ActivityTitle title;
	private ViewPager mViewPager;
	private int mCurrentIndex = 498;
	private CalendarCard[] mShowViews;
	private CalendarViewAdapter<CalendarCard> adapter;
	private SildeDirection mDirection = SildeDirection.NO_SILDE;

	// private SettingSimpleView applySpecialise, openColumn, issueWikipedia,
	// savePrescription;

	enum SildeDirection {
		RIGHT, LEFT, NO_SILDE;
	}

	private ImageButton preImgBtn, nextImgBtn;
	private TextView monthText, integralTextView, coinTextView;
	private TextView getintegralteTextView;
	private Button closeImgBtn;
	JSONArray integralList;
	ArrayList<ThiredTextItemView> list;

	ThiredTextItemView inviteFriend, read, goodReputation, question, answer, social, shareRead, reward, Praise, CommentsOnTheThumbUp;
    TextView textView;//描述兑换比例
   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_assignmentcenter);
		
		initView();
		getdata();

	}

	private void getdata() {
		long time = System.currentTimeMillis();
		signin(time);
	}

	/**
	 * 初始化
	 * 
	 * 
	 * @author lkx Created at 2015-10-9 下午2:37:28
	 */
	private void initView() {
		mViewPager = (ViewPager) this.findViewById(R.id.vp_calendar);
		monthText = (TextView) this.findViewById(R.id.tvCurrentMonth);
		title = (ActivityTitle) findViewById(R.id.AssignmentCenteringTitle);
		title.setTitle("任务中心");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		getintegralteTextView = (TextView) findViewById(R.id.textView5);
	
		integralTextView = (TextView) findViewById(R.id.textView3);
		coinTextView = (TextView) findViewById(R.id.textView1);

		inviteFriend = (ThiredTextItemView) findViewById(R.id.inviteFriend);
		read = (ThiredTextItemView) findViewById(R.id.read);
		goodReputation = (ThiredTextItemView) findViewById(R.id.goodReputation);
		question = (ThiredTextItemView) findViewById(R.id.question);
		answer = (ThiredTextItemView) findViewById(R.id.answer);
		social = (ThiredTextItemView) findViewById(R.id.social);
		shareRead = (ThiredTextItemView) findViewById(R.id.shareRead);
		reward = (ThiredTextItemView) findViewById(R.id.reward);
		Praise = (ThiredTextItemView) findViewById(R.id.Praise);
		textView=(TextView) findViewById(R.id.textView8);
		CommentsOnTheThumbUp = (ThiredTextItemView) findViewById(R.id.CommentsOnTheThumbUp);
		// String text=  SharedPreferencesKit.getJsonObject(this, Const.SP_KEY_CONFIG_JSON).getString("");
	//	textView.setText(text);
		findViewById(R.id.applyForExpert).setOnClickListener(this);
		findViewById(R.id.openColumn).setOnClickListener(this);
		findViewById(R.id.issueWikipedia).setOnClickListener(this);
		findViewById(R.id.savePrescription).setOnClickListener(this);
		list = new ArrayList<ThiredTextItemView>();
		list.add(inviteFriend);
		list.add(read);
		list.add(goodReputation);
		list.add(question);
		list.add(answer);
		list.add(social);
		list.add(shareRead);
		list.add(reward);
		list.add(Praise);
		list.add(CommentsOnTheThumbUp);

		CalendarCard[] views = new CalendarCard[3];
		for (int i = 0; i < 3; i++) {
			views[i] = new CalendarCard(this, this);
		}
		adapter = new CalendarViewAdapter<CalendarCard>(views);
		setViewPager();

		LinearLayout date = (LinearLayout) findViewById(R.id.id_date);
		date.setVisibility(View.GONE);// 日历暂时隐藏
	}

	/**
	 * 
	 * 签到
	 * 
	 * 
	 * @author yyh Created at 2015-10-27 下午4:10:09
	 */
	protected void signin(long time) {
		Api.post("todaySignIn", ApiParam.create().addParam("time", time), new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				// TODO Auto-generated method stub

				try {
					int integral = result.getData().getInt("integral");
					int coin = result.getData().getInt("gold");

					integralTextView.setText(integral + "");
					coinTextView.setText(coin + "");
					textView.setText(result.getData().getString("scoreChangeGold"));

					integralList = result.getData().getJSONArray("integralList");
					for (int i = 0; i < integralList.length(); i++) {

						String title = integralList.getJSONObject(i).getString("title");
						int limit = integralList.getJSONObject(i).getInt("limit");
						int score = integralList.getJSONObject(i).getInt("score");
						int number = integralList.getJSONObject(i).getInt("num");
						if(integralList.getJSONObject(i).getString("integralCode").equals("TodaySignIntegral")){
							getintegralteTextView.setText("今日签到+"+score+"积分");
						}
						setdata(list.get(i), title, limit, score, number);

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				Log.e("signinfire", msg);

			}
		}, this);
	}

	/**
	 * 
	 * 任务列表填充数据
	 * 
	 * 
	 * @author yyh Created at 2015-10-27 下午5:43:29
	 */
	protected void setdata(ThiredTextItemView view, String title, int limit, int score, int number) {
		// TODO Auto-generated method stub
		view.setVisibility(view.VISIBLE);
		view.setItemName(title);
		if (limit == 0) {
			view.setCentertext("无限制");
		} else {
			view.setCentertext(number + "/" + limit);

		}

		int getscore = score * number;
		if (score > 0) {
			view.setRighttext("+" + score + "," + "+" + getscore + "积分");
		} else {
			view.setRighttext(score + "," + "-" + getscore + "积分");
		}

	}

	private void setViewPager() {
		mViewPager.setAdapter(adapter);
		mViewPager.setCurrentItem(498);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				measureDirection(position);
				updateCalendarView(position);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void clickDate(CustomDate date) {
	}

	@Override
	public void changeDate(CustomDate date) {
		monthText.setText(date.toString());
	}

	/**
	 * 计算方向
	 * 
	 * @param arg0
	 */
	private void measureDirection(int arg0) {

		if (arg0 > mCurrentIndex) {
			mDirection = SildeDirection.RIGHT;

		} else if (arg0 < mCurrentIndex) {
			mDirection = SildeDirection.LEFT;
		}
		mCurrentIndex = arg0;
	}

	// 更新日历视图
	private void updateCalendarView(int arg0) {
		mShowViews = adapter.getAllItems();
		if (mDirection == SildeDirection.RIGHT) {
			mShowViews[arg0 % mShowViews.length].rightSlide();
		} else if (mDirection == SildeDirection.LEFT) {
			mShowViews[arg0 % mShowViews.length].leftSlide();
		}
		mDirection = SildeDirection.NO_SILDE;
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.applyForExpert:
			applyTest("1", ApplyBeSpecialist.class);
			break;

		case R.id.openColumn:
			applyTest("2", ApplyForColumnActivity.class);
			break;
		case R.id.issueWikipedia:
			ToastKit.showShort(AssignmentCenteringActivity.this, "建设中");
			break;
		case R.id.savePrescription:
			ToastKit.showShort(AssignmentCenteringActivity.this, "建设中");
			break;

		}

	}

	private void applyTest(String string, final Class class1) {

		Api.get("apply/applyState", ApiParam.create().addParam("type", string), new ApiCallback(AssignmentCenteringActivity.this, false) {

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					switch (result.getData().getInt("state")) {
					case 1:

						Intent intent = new Intent(AssignmentCenteringActivity.this, class1);
						startActivity(intent);
						break;

					case 2:
						ToastKit.showShort(getApplicationContext(), "您的申请正在审核");
						break;
					case 3:
						ToastKit.showShort(getApplicationContext(), "您的申请已经通过");
						break;
					case 4:
						ToastKit.showShort(getApplicationContext(), "对不起，您的等级还不能申请该项");
						break;
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub

			}
		}, AssignmentCenteringActivity.this);

	}

}